$(function(){	 
	var map = QXMap.Map('container', {
        zoom:10,
        center: [120.260,30.086],
        maptype: QMAP_NORMAL_MAP
    });
    $.ajax({
		url:"../data/萧山_all.xml",
		data:"json",
		success:function(data){
			var points=$(data).find("Area").attr("Coords").split(",")
			for(var i=0,arr=[];i<points.length;i++){
				if(i%2==0){
					arr.push({lng:points[i],lat:points[i+1]})
				}
			}
			var polylineOptions =
			   {
			       color: 'red', 
			       weight: 3,
			       fillColor: 'red',
			       opacity: 0.7,
			       fillopacity: 0.8,
			       isfill: false
			   }
			var polyline = QXMap.PolyLine(arr, polylineOptions);
			map.addVectorLayer(polyline);
			//map.setCenter({lng:120.260,lat:30.186})

		}
	})
    //大风灾害函数
	function wind(num){
		var time=getTime();
		$(".pubTime input").val(time)
		time=time.replace(/\//g,"");
		time=time.replace(" ","")
			
		time=time.replace(/:/g,"")+"00"	
		var mapProductOptions={	
				productcategory: 'windv',
				time: time,
				regioncode: '330000',
				method: 'NAFP_FOR_FTM_OCF_ZJ',
				interface: 'getContourEleByTimeAndRegionCode',
				key: 'zjwlzx',
				pwd: '123',
				level: 0,
				validtime: num, 
				gridmethod: 'NAFP_FOR_FTM_OCF_ZJ',
				gridinterface: 'getGridForecastEleGridInRectByTimeAndLevelAndValidtime',
				nlat: 55,
				slat: 18,
				wlon: 116,
				elon: 125,
				isshowzero: false,
				isshowcontrol: true,
				position: "topright",
				isforecast:true
		}
		 var product = QXMap.addVectorMapProduct(mapProductOptions, function (data) {
		 	var da=data._data;
		 	var arr=[];
		 	// arr.push(da[0]);
		 	// 	for(var i=1;i<da.length;i++){
		 	// 		for(var j=0;j<arr.length;j++){
		 	// 			console.log(da[i].color==arr[j].color)
		 	// 			if(da[i].color==arr[j].color){
		 	// 				break
		 	// 			}

		 	// 		}
		 	// 		arr.push(da[i])	
		 			
		 	// 	}
		 	// console.log(arr)

				// var array = [1,1,2,2,3,4,5,5,6,7,7];
				var r = [];
				for(var i = 0; i < da.length; i++) {
					for(var j = i + 1; j < da.length; j++)
						if(da[i].color === da[j].color) j = ++i;
					r.push(da[i]);
				}
				// alert(r);
				console.log(r)
				// for(var i=0;i<r.length;i++){

				// }
				addColorCode("风力速度",r)
        if (data == null) return;
    });

		// var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/RADA_L3_MST_Hail/<getContourValidti></getContourValidti>meListByTime?userid=zjwlzx&pwd=123&<productcatego></productcatego>ry=hail&fcstlevel=0&time=20170821124000&timetype=D&<admin></admin>codes=330000&elements=time,hours&limitcnt=100&dataformat=json'
		// $.ajax({
		// 	url:ajaxUrl,
		// 	type:"get",
		// 	success:function(data){
		// 		console.log(data)
		// 	}
		// })
	}
	wind(21);
	drawAxis(15,3);
	removeAxis()
	//获得时间
	function getTime(){
		var date = new Date();
		date.setDate(date.getDate()-1)
		var Day = date.getDate()>9?date.getDate():"0"+date.getDate();
		var Year = date.getFullYear();
		var Mouth = date.getMonth()>=9?date.getMonth()+1:"0"+(date.getMonth()+1);
		var end=Year+"/"+Mouth+"/"+Day+" "+"12"+":"+"00";
		return end;
	};
	// 生成时间轴
	function drawAxis(initTime,step){
		var html="";
		var j=0;
		var k=0;
		var date=new Date();
		date.setDate(date.getDate()-1)
		
		for(var i=0;i<=120;i+=3){
			var time=initTime+j*step
			j++
			html+='<li data-toggle="'+i+'">'+
			'<span class="upRect"></span>'+
			'<span>'+(time>23?00:time)+':00</span>'
			if(time>=23){
				date.setDate(date.getDate()+1)
				html+='<i ></i>'+
				'<b class="changeDay">'+(date.getMonth()+1)+"/"+date.getDate()+'</b>';
				initTime=3;
				j=0;
			}
			html+='</li>'
			
		}
		$(".changeTime ul").html(html);
		removeAxis()
	}
	//时间切换函数
	function removeAxis(){
		var index=0;
		$(".changeTime").unbind();
		$(".changeTime").on("click","button",function(e){
			e.preventDefault();
			var len=$(".changeTime ul li").length
			if($(this).hasClass("changeNext")){
				$(this).siblings("button").prop("disabled",false)
				$(".changeTime ul li").eq(index).addClass("displayNone").removeClass("displayBlock")
				$(".changeTime ul li").eq(++index).addClass("active").siblings(".active").removeClass("active")
				if(len-index<=8){$(this).prop("disabled",true)}
					wind($(".changeTime ul li").eq(index).attr("data-toggle"))
				console.log("changeNext",$(".changeTime ul li").length)
			}else{
				--index
				$(this).siblings("button").prop("disabled",false)
				$(".changeTime ul li").eq(index)
				$(".changeTime ul li").eq(index).removeClass("displayNone").addClass("displayBlock").addClass("active").siblings(".active").removeClass("active")
				if(index==0){
					$(this).prop("disabled",true);
				}
				wind($(".changeTime ul li").eq(index).attr("data-toggle"))
				console.log("changeprec",index)
			}
		})
		$(".changeTime").on("mouseenter","li",function(){
			$(this).addClass("active").siblings(".active").removeClass("active");
			var num=$(this).attr("data-toggle")
			console.log()
			wind(num)
		})
		$(".changeTime").on("mouseleave","li",function(){
			$(this).removeClass("active");
		})
	}
	function addColorCode(title,colorCode){
		var html=' <p>'+title+'</p><ul>'
		for(var i=0;i<colorCode.length;i++){
			var r=colorCode[i].color.lastIndexOf(",")
			console.log()
			html+='<li>'+
                            '<span  style="background: rgb('+colorCode[i].color.slice(0,r)+')"></span>'+
                            '<span>'+colorCode[i].value+' m/s</span>'+
                    '</li>'
		}
		html+="</ul>"
		$(".colorCode").html(html)                 
	}
	

})